/////////////
//	CBList.cpp		:	v0010
//	Written by		:	Li Haijun
//	Compiler		:	Microsoft Visual C++ 4.0 & DirectX
//	Library			:	DDraw.Lib
//	Copyright (C)	:	1996 WayAhead Corporation 
//	v0010			:	Dec.2.1996
/////////////
// implementation file

// This file provide basic interfaces for deal with bitmap of interface 
//////////////////////////////

#include "stdafx.h"
#include <stdio.h>

#include <ddraw.h>
#include "DDCompo.h"
#include "DsApi.h"	// sound
#include	"Assert.h"

#include "Marco.h"
#include "DDList.h"

////////////////////////////
#include "l_allbmp.h"
extern char	bmpfilename[30];
extern char	listfilename[30];
/////////////////////////////////////

/////////////////////////////////////
//	to restore all button's pointer
class	CDDListUnit	*	pAllListUnit[MAX_LISTUNIT];

class	CDDList *	pCurrentList;	// a pointer point to the current button
int		nCurrentListState = NONE;	// current List's state,0-up,1-down,2-disable,3-focus,4-disable,5-hide

WAVEDYNAMIC Wave_List;	  // to broadcast a sound when click a button

//constructor
CDDList::CDDList()
{
	m_nFileNameId = NONE;
	m_szOneBitmap.cx = 0L;
	m_szOneBitmap.cy = 0L;

	m_rTotal.left	= 0L;
	m_rTotal.top	= 0L;
	m_rTotal.right	= 0L;
	m_rTotal.bottom	= 0L;

	m_rCurrent.left	= 0L;
	m_rCurrent.top	= 0L;
	m_rCurrent.right	= 0L;
	m_rCurrent.bottom	= 0L;

	m_dwX = 0;	m_dwY = 0;

	m_nState = LIST_UP;
	m_nPreviousState = 0;
	m_nPart = 0;

	m_dTextX = 20 ;
	m_dTextY = 12 ;
	memset( m_ShowString, 0, sizeof(m_ShowString) );
	m_String.BeginText( 100, 1 ) ;

	m_bEnableSound = TRUE;

	m_bSerFlag = FALSE ;
	m_dSerialX = 30 ;
	m_dSerialY = 12 ;		// to store the offset that the letters should be placed relative to the top-left point of this list
	memset( m_ShowSer, 0, sizeof(m_ShowSer) );
	m_Serial.BeginText( 4, 1 ) ;
}
//constructor
CDDList::~CDDList()
{
	// to end the text
	m_String.EndText();
	m_Serial.EndText();
	// to release the bitmap of this list
	Release();
}

BOOL CDDList::PreLoad(int FilenameId, int x, int y,
						int OneBitmapWidth/*=0*/, int OneBitmapHeight/*=0*/)
{
	char	fname[20];

	HRESULT			ddrval;
	DDSURFACEDESC	ddsd;

//////////////////////////////////////////////////
	_itoa( FilenameId, fname, 10 );

	class CPicture_imageall picture;

	picture.image_open_compress(bmpfilename);
	picture.image_open_index(listfilename);
	picture.LoadBitmap( &m_BitmapSurface, FilenameId );
	picture.image_close_index();
	picture.image_close_compress();

/////////////////////////////////////////////////////////
	// to set the value of m_FileName
	m_nFileNameId = FilenameId;	//set the member veriable m_FileName
    //
    // get size of surface.
    //
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH;

    ddrval = m_BitmapSurface.GetSurface()->GetSurfaceDesc(&ddsd);
	if(ddrval != DD_OK)
		return FALSE;
	m_rTotal.left = 0;
	m_rTotal.top = 0;
	m_rTotal.right = ddsd.dwWidth;
	m_rTotal.bottom = ddsd.dwHeight;
// to set the value of m_szOneBitmap
	if( (OneBitmapWidth==0)&&(OneBitmapHeight==0) )
	{
		// I assume that all single bitmaps' height is equal to it's width
		if(m_rTotal.bottom > m_rTotal.right)
			m_szOneBitmap.cx = m_szOneBitmap.cy = m_rTotal.right;
		else
			m_szOneBitmap.cx = m_szOneBitmap.cy = m_rTotal.bottom;
		// to set the value of m_rCurrent
		m_rCurrent.left = m_rCurrent.top = 0;
		m_rCurrent.right = m_rCurrent.bottom = m_szOneBitmap.cx;
	}
	else if( OneBitmapWidth * OneBitmapHeight == 0 )
		return FALSE;
	else
	{
		// I assume that all single bitmaps' height is not equal to it's width
		m_szOneBitmap.cx = OneBitmapWidth;
		m_szOneBitmap.cy = OneBitmapHeight;
	}
//to set the value of m_dwX, m_dwY
	m_dwX = x; m_dwY = y;

	SetState(LIST_UP);

	return TRUE;
}
// to release this button's pointer
void CDDList::Release()
{
	// release the surface that this button's bitmap used
	m_BitmapSurface.Release();
}
// to set this list's id
void CDDList::SetID(int ID)
{
	m_nID = ID ;	// button id
}

void CDDList::SetState(int state)	// let button up or down or ...
{
	m_nState = state ;

	if( m_szOneBitmap.cx == m_rTotal.right )
	{
		m_rCurrent.left = m_rTotal.left;
		m_rCurrent.top = m_rTotal.top + m_nState * m_szOneBitmap.cy;
		m_rCurrent.right = m_rCurrent.left + m_szOneBitmap.cx;
		m_rCurrent.bottom = m_rCurrent.top + m_szOneBitmap.cy;
	}
	else
	{
		m_rCurrent.left = m_rTotal.left + m_nState * m_szOneBitmap.cx;
		m_rCurrent.top = m_rTotal.top;
		m_rCurrent.right = m_rCurrent.left + m_szOneBitmap.cx;
		m_rCurrent.bottom = m_rCurrent.top + m_szOneBitmap.cy;
	}
// to set the string color of this list
	if( state == LIST_FOCUS )
		m_String.SetTextColor( RGB(218, 177, 95) );
	else if( state == LIST_DOWN )
		m_String.SetTextColor( RGB(252, 247, 147) );
	else if( state == LIST_UP )
		m_String.SetTextColor( RGB(218, 177, 95) );
	else
		m_String.SetTextColor( RGB(128, 128, 128) );
}
// to set the serial number of this list
void	CDDList::SetSerial( LPCTSTR serial )
{
	if(serial[0] != '\0')
	{
		memset(m_ShowSer, 0, sizeof(m_ShowSer)) ;
		m_bSerFlag = TRUE ;
		strcpy(m_ShowSer, serial) ;
	}
}

//to test if the given point on this button
BOOL CDDList::OnList( int x, int y)
{
	int	m_nLeftEdge = m_dwX;
	int m_nRightEdge = m_nLeftEdge + (int)m_szOneBitmap.cx;
	int m_nTopEdge = m_dwY;
	int m_nBottomEdge = m_nTopEdge + (int)m_szOneBitmap.cy;

	if( (x >= m_nLeftEdge)&&(x <= m_nRightEdge )
	  &&(y >= m_nTopEdge )&&(y <= m_nBottomEdge) )
		return TRUE;
	else
		return FALSE;
}
////////////////////////////////////////////
//	to show the string of this list
void CDDList::BlitString()
{
	// to blit the serial number of this list
	if( m_bSerFlag == TRUE )
	{
		m_Serial.MyTextOut( DD_GetBackBuffer(), m_dwX+m_dSerialX, m_dwY+m_dSerialY, m_ShowSer );
		m_Serial.GetDDSurface()->Erase();
	}
// to get a surface for you string
//	CDDText string;

/*	if( ! m_String.BeginText( 100, 1 ) )
	{
	}
*/
//	string.GetSurface()->Erase();
	m_String.MyTextOut( DD_GetBackBuffer(), m_dwX+m_dTextX, m_dwY+m_dTextY, m_ShowString );
	m_String.GetDDSurface()->Erase();
	// release the CDDText instance string
//	m_String.EndText();
}
///////////////////////////
//	 to show this list
void CDDList::Show()
{
/*	POINT	ptDest;

	ptDest.x = m_dwX;
	ptDest.y = m_dwY;
	
	if( (m_nState != LIST_HIDE)&&(m_nState != LIST_DISABLE) )
	{
		m_BitmapSurface.BltToBack(ptDest, &m_rCurrent);

		RECT	ShowRect;
		ShowRect.left = m_dwX,	ShowRect.top = m_dwY;
		ShowRect.right = m_dwX + m_szOneBitmap.cx;
		ShowRect.bottom = m_dwY + m_szOneBitmap.cy;
		DDC_UpdateScreen( &ShowRect );
	}		*/
	Blit();
	Update();
}
// to blit the current part of image to the back buffer
void CDDList::Blit(void)
{
	POINT	ptDest;

	ptDest.x = m_dwX;
	ptDest.y = m_dwY;

//	if( (m_nState != LIST_HIDE)&&(m_nState != LIST_DISABLE) )
	if( m_nState != LIST_HIDE )
	{
//		SetState( m_nState ) ;
		m_BitmapSurface.BltToBack(ptDest, &m_rCurrent);
	}
// to blit the string of this list to the back buffer.
	BlitString();
}
// to update the area of current part of image from back buffer to front buffer
void CDDList::Update(void)
{
//	if( (m_nState != LIST_HIDE)&&(m_nState != LIST_DISABLE) )
	if( m_nState != LIST_HIDE )
	{
		RECT	ShowRect;
		ShowRect.left = m_dwX,	ShowRect.top = m_dwY;
		ShowRect.right = m_dwX + m_szOneBitmap.cx;
		ShowRect.bottom = m_dwY + m_szOneBitmap.cy;
		DDC_UpdateScreen( &ShowRect );
	}
}

///////////////////////////////////////////////////
// to define all member function of CDDListUnit
///////////////////////////////////////////////////
// construct
CDDListUnit::CDDListUnit()
{
	m_nSerialNum = NONE;

	for( int i=0; i<MAX_LISTINUNIT; i++ )
	{
		m_pList[i] = NULL;
	}
}
//	disconstruc
CDDListUnit::~CDDListUnit()
{
	Release();
	m_nSerialNum = NONE;

}
// to set state of each LIST that in this list-unit
void	CDDListUnit::SetMemberState( int state )
{
	for( int i=0; i<MAX_LISTINUNIT; i++ )
	{
		if( (m_pList[i] != NULL)&&(m_pList[i]->GetState() != LIST_DISABLE) )
		{
			m_pList[i]->SetState(state);
			m_pList[i]->Show();
		}
	}
}
// to show this list unit
void	CDDListUnit::Show( )
{
/*	for( int i=0; i<MAX_LISTINUNIT; i++ )
	{
		if( m_pList[i] != NULL )
		{
			m_pList[i]->SetState(LIST_UP);
			m_pList[i]->Show();
		}	
	}		*/
	Blit();
	Update();
}
// to blit this list unit
void	CDDListUnit::Blit( )
{
	for( int i=0; i<MAX_LISTINUNIT; i++ )
	{
		if( m_pList[i] != NULL )
		{
			m_pList[i]->Blit();
		}	
	}
}
// to update all the lists of this unit
void	CDDListUnit::Update( )
{
	for( int i=0; i<MAX_LISTINUNIT; i++ )
	{
		if( m_pList[i] != NULL )
		{
			m_pList[i]->Update();
		}	
	}
}
// to release all lists of this unit
void	CDDListUnit::Release()
{
	for( int i=0; i<MAX_LISTINUNIT; i++ )
	{
		if( m_pList[i] != NULL )
		{
			delete m_pList[i];
			m_pList[i] = NULL;
		}
	}
}
/////////////////////////////////////////
// to test whether a button is clicked ----WM_LBUTTONDOWN
/////////////////////////////////////////
BOOL	LeftButtonDownOnList(int xPos, int yPos)
{
	for( int i=0; i<MAX_LISTUNIT; i++ )
	{
		if( pAllListUnit[i] != NULL	)
		{
			for( int j=0; j<MAX_LISTINUNIT; j++ )
			{
				if( (pAllListUnit[i]->m_pList[j] != NULL)
				  &&(pAllListUnit[i]->m_pList[j]->GetState() != LIST_DISABLE)
				  &&(pAllListUnit[i]->m_pList[j]->OnList(xPos, yPos) ) )
				{
					if( pAllListUnit[i]->m_pList[j]->GetState() == LIST_FOCUS )
					{
						pAllListUnit[i]->SetMemberState(LIST_UP);
						// set the just been clicked button's state
						pAllListUnit[i]->m_pList[j]->SetState(LIST_DOWN);
						pAllListUnit[i]->m_pList[j]->Show();
						pAllListUnit[i]->m_nSerialNum = j ; 
						// to set the global button's state
						pCurrentList = pAllListUnit[i]->m_pList[j];
						nCurrentListState = LIST_DOWN;
					}
					strcpy( Wave_List.strFileName, "sound\\test.wav" );
					Wave_List.Play(0);
					return TRUE;
				}
			}
		}
	}
	return	FALSE;
}
///////////////////////////////////////////////////////////
// to test whether cursor is moved on a List ----- WM_ONMOUSEMOVE
///////////////////////////////////////////////////////////
BOOL	MouseMoveOnList(int xPos, int yPos)
{
	for( int m=0; m<MAX_LISTUNIT; m++ )
	{
		if( pAllListUnit[m] != NULL	)
		{
			for( int n=0; n<MAX_LISTINUNIT; n++ )
			{
				if( (pAllListUnit[m]->m_pList[n] != NULL)
				  &&(pAllListUnit[m]->m_pList[n]->GetState() == LIST_FOCUS)
				  &&(pAllListUnit[m]->m_pList[n]->OnList(xPos, yPos) == FALSE ) )
				{
					// set the just been clicked button's state
					pAllListUnit[m]->m_pList[n]->SetState(LIST_UP);
					pAllListUnit[m]->m_pList[n]->Show();
				}
			}
		}
	}
	for( int i=0; i<MAX_LISTUNIT; i++ )
	{
		if( pAllListUnit[i] != NULL	)
		{
			for( int j=0; j<MAX_LISTINUNIT; j++ )
			{
				if( (pAllListUnit[i]->m_pList[j] != NULL)
				  &&(pAllListUnit[i]->m_pList[j]->GetState() == LIST_UP)
				  &&(pAllListUnit[i]->m_pList[j]->OnList(xPos, yPos) ) )
				{
					// set the just been clicked button's state
					pAllListUnit[i]->m_pList[j]->SetState(LIST_FOCUS);
					pAllListUnit[i]->m_pList[j]->Show();
					// to set the global button's state
					pCurrentList = pAllListUnit[i]->m_pList[j];
					nCurrentListState = LIST_FOCUS;

					return TRUE;
				}
			}
		}
	}
	return FALSE;
}
////////////////////////////////////////////////////////////////////////////////
void	FACE_DeleteAllList()
{
	for( int i=0; i<MAX_LISTUNIT; i++ )
	{
		if( pAllListUnit[i] != NULL	)
		{
			delete pAllListUnit[i];
			pAllListUnit[i] = NULL;
		}
	}
}
////////////////////////////////////////////////////////////////////////////////
void	FACE_BlitAllList()
{
	for( int i=0; i<MAX_LISTUNIT; i++ )
	{
		if( pAllListUnit[i] != NULL	)
		{
			pAllListUnit[i]->Blit();
		}
	}
}
////////////// END